package com.shiguiwu.springmybatis.javabase.collect;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.shiguiwu.springmybatis.app.entity.User;
import com.shiguiwu.springmybatis.data.json.JsonTests;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: stone
 * @date: Created by 2021/2/9 20:16
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.collect
 */
@Slf4j
public class MapTest {

    public static void main(String[] args) {
        //testTutoolMap();

        testObj2Map();
    }

    public static void testTutoolMap() {
        Map<String, String> build = MapUtil.builder("shiguiwu", "hahaha").build();
        build.put("haha", "haha");

        System.out.println(build);

        String shiguiwu = build.getOrDefault("shig11uiwu", "12344");
        System.out.println(shiguiwu);

        testRepeatKey();

    }


    public static void testMap() {

        Map<String, Object> map = new HashMap<>();

        Object put = map.put("key", "shiguiwu");
        put = map.put("key", "zhoudongxiu");
        System.out.println(put);
        Object key = map.compute("key11", (k, v) -> k + v);
        System.out.println(key);
        Object shi = map.computeIfAbsent("shi", String::length);
        System.out.println(shi);
        Object aaa = map.computeIfPresent("key", (k, v) -> k + v);
        System.out.println(aaa);
        System.out.println(map.containsKey("key"));
        System.out.println(map.containsValue("zhoudongxiu"));

        //同步map
        Map<String, Object> stringObjectMap = Collections.synchronizedMap(map);

    }


    public static void testConcurrentHashMap() {
        Map<String, Object> concurrentHashMap = new ConcurrentHashMap<>();
        concurrentHashMap.put("shiguiwu", "shiguiwu");
        concurrentHashMap.put("shiguiwu1", "shiguiwu");
        System.out.println(concurrentHashMap);

    }

    /**
     * list转map,重复key，value做拼接
     */
    public static void testRepeatKey() {


        User u1 = new User();
        u1.setUsername("shiguiwu");
        u1.setPhone("18676894410");

        User u2 = new User();
        u2.setUsername("shiguiwu");
        //u2.setPhone("15622311804");

        User u3 = new User();
        u3.setUsername("zhoudongxiu");
        u3.setPhone("17674655232");

        List<User> list = new ArrayList<>();
        list.add(u1);
        list.add(u2);
        list.add(u3);


        //会有空指针异常
        Map<String, String> stringMap = list.stream().peek(e -> {
            if (StrUtil.isBlank(e.getPhone())) {
                e.setPhone("");
            }
        }).collect(Collectors.toMap(User::getUsername, User::getPhone, (oValue, nValue) -> oValue.concat(",").concat(nValue)));

        log.info("list转map,重复key的问题 ===> {}", JSONUtil.toJsonStr(stringMap));
    }


    /**
     * obj 转map ,hutools工具没有bug
     */
    public static void testObj2Map() {
        JsonTests.Param p = new JsonTests.Param("小狗1", 234.53, BigDecimal.valueOf(99.44), 55L);
        Map<String, Object> map = BeanUtil.beanToMap(p);

        log.info("map ===> {}", JSONUtil.toJsonStr(map));

        JsonTests.Param bean = BeanUtil.toBean(map, JsonTests.Param.class);

        log.info("是否相等 ===> {}", p == bean);
    }
}
